//
//  InputPasswordView.swift
//  todoios
//
//  Created by qianli on 2025/5/22.
//

import Foundation

class InputPasswordView: UIView {
    // 密码可见性按钮
    private lazy var visibilityButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
        button.setImage(UIImage(systemName: "eye.fill"), for: .selected)
        button.tintColor = .systemGray
        button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        button.addTarget(self, action: #selector(LZgYLiT9Kg7BzGoQ), for: .touchUpInside)
        return button
    }()
    
    private lazy var edit: UITextField = {
        let filed = UITextField()
        return filed
    }()

    init(frame: CGRect,placeholder:String=LocalizedString("PLEASE INPUT PASSWORD")) {
        super.init(frame: frame)
        GHL4Rhb91XYBer27(placeholder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        GHL4Rhb91XYBer27(LocalizedString("PLEASE INPUT PASSWORD"))
    }
//
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    private func GHL4Rhb91XYBer27(_ placeholder:String=LocalizedString("PLEASE INPUT PASSWORD")){
        let icoView = UIImageView(frame: CGRect(x: 15, y: self.bounds.height/2-20/2, width: 20, height: 20))
//        self.addSubview(icoView)
        var leftImage = "qianli_field_pwd".qianli_R_image
        if let image = leftImage?.withRenderingMode(.alwaysTemplate) {
//            icoView.image = image
//            icoView.tintColor = .label
            leftImage = image
        }else{
//            icoView.image = UIImage(named: "qianli_field_pwd")
        }
        
        let edtWidth =  self.bounds.width-30
        let edtHeight = self.bounds.height/2
        self.addSubview(edit)
        edit.frame = CGRect(x: 15, y: self.bounds.height/4, width: edtWidth, height: edtHeight)
        edit.placeholder = placeholder //"请输入密码"
        edit.font = .systemFont(ofSize: 15)
        edit.isSecureTextEntry = true // 默认隐藏密码
        edit.rightView = visibilityButton
        edit.rightViewMode = .always
        edit.show_qianli_filed_lefticon(leftImage, frame: CGRect(x: 0, y: 0, width: edtHeight, height: edtHeight), imageSize: CGSize(width: 20, height: 20),tintColor:UIColor.label)
        
    }
    
    @objc private func LZgYLiT9Kg7BzGoQ() {
        // 切换密码可见性
        edit.isSecureTextEntry.toggle()
        visibilityButton.isSelected = !edit.isSecureTextEntry
        
        // 保持光标在文本末尾
        if let textRange = edit.textRange(from: edit.endOfDocument, to: edit.endOfDocument) {
            edit.selectedTextRange = textRange
        }
    }
    
    func JRrMFFk8vXgbNlUN()->String{
        return edit.text ?? ""
    }
}
